<?php
/**
 * Mqtt推送服务
 * zhouliang
 * 2020.09.06
 */

namespace App\Services\Tools;

use App\Exceptions\BasicException;
use App\Repositories\LogRepository;
use App\Services\RoomScore\RoomUserGiftScoreService;
use Bluerhinos\phpMQTT;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class PushService
{

    public $senderId = 'system';

    const MSG_TYPE_TRANSFER = 'HS:TransferMsg';
    const MSG_TYPE_DATING = 'HS:DatingMsg';

    const PUSH_BROADCAST = 5000;//广播

    const PUSH_POPULARITY = 5001;//推送房间-人气变化
    const PUSH_RIDE = 5003;//推送房间-坐骑进场特效
    const PUSH_NOBILITY = 5004;//推送房间-爵位用户进场特效
    const PUSH_APPLY_COUNT = 5005;//推送房间-上麦申请人数变化
    const PUSH_BANNED_USER = 5007;//推送房间-用户禁言 1禁言2解禁
    const PUSH_CLOSE_PIT = 5011;//推送房间-是否封麦 1封麦2解封
    const PUSH_CLEAR_CARDIAC = 5013;//推送房间-清空单个麦位心动值
    const PUSH_CLEAR_CARDIAC_ALL = 5014;//推送房间-清空所有麦位心动值
    const PUSH_SET_MANAGER = 5015;//推送房间-设置房间管理员
    const PUSH_DELETE_MANAGER = 5016;//推送房间-删除房间管理员
    const PUSH_SWITCH_VOICE = 5017;//推送房间-开关麦 1开0关
    const PUSH_GIFT_BANNER = 5019;//推送所有人-横幅礼物通知
    const PUSH_GIFT_CHAT_ROOM = 5020;//推送房间-聊天室礼物通知
    const PUSH_FISH = 5021;//推送所有人-小猫钓鱼钓到大礼物时通知
    const PUSH_ROOM_PASSWORD = 5022;//推送房间-房间密码变化通知 0取消密码1设置或修改密码
    const PUSH_CARDIAC_SWITCH = 5023;//推送房间-房间心动值开关变化通知 1开2关
    const PUSH_ROOM_WHEAT = 5024;//推送房间-上麦模式变化通知 1自由2排麦
    const PUSH_UPDATE_ROOM_NAME = 5025;//推送房间-修改房间名称
    const PUSH_WEEK_STAR = 5027;//推送房间-周星用户进场特效
    const PUSH_UPDATE_ROOM_BACKGROUND = 5028;//推送房间-修改房间背景
    const PUSH_UPDATE_ROOM_PLAYING = 5029;//推送房间-修改房间公告
    const PUSH_BOSS_ATTACK = 5030;//推送房间-BOSS大作战 超过99999金币 发送所有人消息
    const PUSH_BOSS_BLOOD = 5031;//推送所有正在攻击boss的用户-boss大作战血量变化推送
    const PUSH_PIT_ON = 5032;//推送房间-上麦
    const PUSH_PIT_DOWN = 5033;//推送房间-下麦
    const PUSH_KICK_OUT = 5034;//推送单独用户-被踢出房间
    const PUSH_APPLY_USER = 5035;//推送单独用户-定向推向给上麦的用户
    const PUSH_SHUT_UP = 5036;//推送房间-用户禁麦 1禁麦2解禁
    const PUSH_JOIN_ROOM = 5037;//推送房间-用户进入房间
    const PUSH_COUNT_DOWN = 5038;//推送房间-麦位倒计时
    const PUSH_ROLL = 5039;//推送房间-扔骰子
    const PUSH_FM_GOLD = 5040;//推送所有房间-电台房开通黄金守护
    const PUSH_FACE = 5041;//推送房间-在麦上发送表情
    const PUSH_ZEGO_LOG = 5042;//推送单独用户-上传即构日志
    const PUSH_ROOM_CHAT_STATUS = 5043;//推送房间-公屏状态
    const PUSH_BALL_START = 5044;//推送房间-球球大作战-开球
    const PUSH_BALL_THROW = 5045;//推送房间-球球大作战-弃球
    const PUSH_BALL_SHOW = 5046;//推送房间-球球大作战-亮球
    const PUSH_SOUND_EFFECT_CHANGE = 5047;//推送房间-房间音效改变
    const PUSH_ORDER_RECEIVE = 5048;//推送大神用户-有用户下单
    const PUSH_ORDER_REFUND = 5049;//推送给大神-有用户要退款
    const PUSH_ROOM_DEMAND_BOSS = 5050;//推送单独用户-推送给8号麦的老板
    const PUSH_ROOM_DEMAND_UPDATE = 5051;//推送房间-更新派单状态
    const PUSH_ROOM_DEMAND_TO_ANCHOR = 5052;//推送所有用户-将派单需求推送给所有符合条件的大神
    const PUSH_ORDER_MESSAGE = 5053;//推送所有的订单消息(浮窗形式)
    const PUSH_OFFER_AUCTION = 5055;//推送房间-拍卖报价
    const PUSH_ABORT_AUCTION = 5056;//推送房间-拍卖流拍
    const PUSH_END_AUCTION = 5057;//推送房间-拍卖成功
    const PUSH_START_AUCTION = 5058;//推送房间-拍卖开始
    const PUSH_CONNECTION_CP_APPLY = 5060;//推送单独用户-CP绑定申请
    const PUSH_CONNECTION_MARRY_APPLY = 5061;//推送单独用户-结婚申请
    const PUSH_JOINROOM_NOTICE = 5062;//人脉推送单独用户-阶级四进入房间通知
    const PUSH_SINGLE_DRAW = 5070;//推送房间-开始抽奖倒计时
    const PUSH_DRAW_RESULT = 5071;//推送房间-开奖结果
    const PUSH_DRAW_RESULT_OWNER = 5072;//推送房主-开奖结果
    const PUSH_THEME_STRIVE_HEADLINE = 5073;//推送话题广场抢头条通知
    const PUSH_THEME_JOIN_USER_NUMBER = 5074;//推送话题广场参与人数变化
    const PUSH_SONG_SWITCH = 5075;//推送房间-主播更新点歌状态
    const PUSH_NOBILITY_COUNT = 5076;//推送房间-单人房贵宾人数变化
    const PUSH_SINGLE_SONG_UPDATE = 5077;//推送房间-单人房歌单变化
    const PUSH_LUCKY_TREE = 5054;//推送所有人-许愿树获得大礼物时通知
    const PUSH_KTV_SING_END = 8012; // ktv 唱歌结束

    const PUSH_ROOM_CONNECTION_ONLINE = 5096;//推送房间-心动连线在线状态
    const PUSH_BLIND_BOX_GIFT_CHAT_ROOM = 5095;//推送房间-盲盒礼物通知

    const ORDER_UN_CHAT_ACCOMPANY = 5205;//主播订单未回复，重新选择主播
    const PUSH_REPORT_LOG = 5093;//推送用户-上报安卓日志


    const PUSH_ROOM_TYPE_CHANGE = 5100;//全服推送-房间信息变更

    const PUSH_TRANSFER = 6200;//转账推送
    const PUSH_REBATE_GIFT_GIVE = 6202;//返利礼物赠送

    const PUSH_MATCH = 6300;//速配推送

    const PUSH_FINISH_WISH = 7004;//推送指定用户-实现心愿
    const PUSH_INTERACTION_FACE = 7011;//推送房间-在麦上发送互动表情
    const PUSH_INTERACTION_SOUND = 7012;//房间互动音效推送
    // 房间pk
    const PUSH_ROOM_PK_MATCH_NOTFOUND = 7100;
    const PUSH_ROOM_PK_MATCH_FINISH = 7101;
    const PUSH_ROOM_PK_MATCH_REPLY = 7102;
    const PUSH_ROOM_PK_MATCH_REPLY_REJECT = 7103;
    const PUSH_ROOM_PK_PLAY = 7104; // 同意
    const PUSH_ROOM_PK_FINISH = 7105;
    const PUSH_ROOM_PK_SCORE = 7106;
    const PUSH_ROOM_PIT_PK_PLAY = 7107;
    const PUSH_ROOM_PIT_PK_FINISH = 7108;
    const PUSH_ROOM_PIT_PK_SCORE = 7109;
    const PUSH_ROOM_PK_MIKE_CHANGE = 7117; // 房间麦克风变化

    const PUSH_RELATION_APPLY = 7301; // 推送关系申请
    const PUSH_REFUSE_RELATION_APPLY = 7302; // 拒绝推送关系申请
    const PUSH_AGREE_RELATION_APPLY = 7303; // 同意推送关系申请

    const PUSH_CP_FIRST_BIND = 7304; //亲密关系推送cp首次绑定
    const PUSH_CONNECT_GIFT = 7305; //亲密度礼物赠送特效推送

    const PUSH_USER_JOIN_SCREEN = 7310;//用户参与我要上公屏活动
    const PUSH_SCREEN_ACTIVITY_END = 7311;//我要上公屏活动结束公示
    const PUSH_CREATE_SCREEN_ACTIVITY = 7312;//创建我要上公屏cmd
    const PUSH_OPENED_SCREEN_ACTIVITY = 7314;//我要上公屏活动结束

    const PUSH_GROUP_JOIN = 8060; // 进群消息
    const PUSH_GROUP_QUIT = 8061; // 退群消息
    const PUSH_GROUP_KICK_OUT = 8062; // 踢出消息
    const PUSH_GROUP_DISBAND = 8063; // 群解散
    const PUSH_GROUP_BANNED = 8064; // 群禁言
    const PUSH_GROUP_UNBANNED = 8065; // 群解除禁言
    const PUSH_GROUP_MEMBER_BANNED = 8066; // 群用户禁言
    const PUSH_GROUP_MEMBER_UNBANNED = 8067; // 群用户解除禁言
    const PUSH_GROUP_CREATE_MANAGER = 8068; // 添加群管理
    const PUSH_GROUP_REMOVE_MANAGER = 8069; // 移除群管理
    const PUSH_GROUP_UPDATE_NAME = 8070; // 修改群昵称
    const PUSH_GROUP_MATCH_PROGRESS = 8080;//速配推送

    public $user_id, $room_id, $topic_room, $topic_client, $delay_seconds;

    const BROADCAST_GASHAPON = 6072;//广播消息-扭蛋
    const BROADCAST_PIRATE = 6073;//广播消息-海盗

    const BROADCAST_LOTTERY_CRYSTAL = 6072;//广播消息-水晶
    const BROADCAST_LOTTERY_CRYSTAL_CRIT = 6073;//广播消息-水晶


    const BROADCAST_LOTTERY_SCREEN = 6074;//广播抽奖-公屏
    const BROADCAST_LOTTERY_BANNER = 6075;//广播抽奖-横幅


    const BROADCAST_NEW_GAME = 6073;//广播消息-海盗

    const BROADCAST_PET_LUCKY_VALUE = 6085;//广播消息-萌宠幸运值g

    const PUSH_GIFT_HALL_COLLECT_SPECIAL = 8101; // 礼物套装集齐特效
    const PUSH_COMMON_BANNER = 8102; // 通用横幅样式， 礼物套装集齐横幅


    const GAME_PUSH_STATUS = 8888;  // 通用 游戏大礼物推送  {系统推送 ， 幸运推送 , 房间推送}
    const GAME_PUSH_FRAGMENT = 4886;  //通用 碎片获取推送
    //用户推送
    const PUSH_USER_GRADE = 5500; //推送用户等级
    /**
     * @var PushChatService
     */
    public $chat;

    public function __construct($user_id = 0, $room_id = 0)
    {
        $this->user_id = $user_id;
        $this->room_id = $room_id;

        $this->topic_room = 'room_' . $this->room_id;
        $this->topic_client = 'user_' . $this->user_id;

        $this->chat = new PushChatService($this->user_id, $this->room_id);
    }

    public function setUser($user_id)
    {
        $this->user_id = $user_id;
        $this->topic_client = 'user_' . $user_id;
    }

    public function setRoom($room_id)
    {
        $this->room_id = $room_id;
        $this->topic_room = 'room_' . $room_id;
    }

    public function setDelay($second)
    {
        $this->delay_seconds = $second;
    }

    /**
     * @param $push_type
     * @param $topic
     * @param array $data
     * @param string $msType
     * @throws \Exception
     * 统一飘屏
     */
    private function pushV1($push_type, $topic, $data = [], $msType = 'RC:CmdMsg')
    {
        $this->rcQueueV1($push_type, $topic, $data, $msType);
    }

    /**
     * 统一飘屏
     * @param $push_type
     * @param $topic
     * @param $data
     * @param $msType
     * @throws \Exception
     */
    private function rcQueueV1($push_type, $topic, $data, $msType)
    {
        /**
         * @var $rcService RongCloudService
         */
        $rcService = app('rongcloud');
        $content = [
            'type' => $push_type,
            'uni_key' => $data['uni_key'],
            'time' => $data['getMillisecond'],
            'msg' => $data,
        ];
        $delay = 0;
        if ($this->delay_seconds > 0) {
            $delay = $this->delay_seconds;
            //            $this->delay_seconds = 0;
        }

        $senderId = $this->getSenderId();
        $rcService->queue($senderId, $topic, ['name' => $topic, 'data' => $content, 'uni_key' => $data['uni_key']], $delay, $msType);
    }

    private function push($push_type, $topic, $data = [], $msType = 'RC:CmdMsg')
    {
        $this->rcQueue($push_type, $topic, $data, $msType);
        // 老的 mqtt 推送
        //        $this->mqttPush($push_type, $topic, $data);
    }

    private function rcQueue($push_type, $topic, $data = [], $msType)
    {
        /**
         * @var $rcService RongCloudService
         */
        $rcService = app('rongcloud');
        $content = [
            'type' => $push_type,
            'uni_key' => uniqidReal(),
//            'time' => (int) (microtime(true) * 1000),
            'time' => getMillisecond(),
            'msg' => $data,
        ];

        $delay = 0;
        if ($this->delay_seconds > 0) {
            $delay = $this->delay_seconds;
            //            $this->delay_seconds = 0;
        }

        $senderId = $this->getSenderId();
        // 当topic为数组时 是推送给多个用户
        $name = is_array($topic) ? $senderId : $topic;
        $rcService->queue($senderId, $topic, ['name' => $name, 'data' => $content], $delay, $msType);
    }

    private function mqttPush($push_type, $topic, $data = [])
    {
        $client_id = 'php-' . uniqid();
        $mqtt = new phpMQTT(env('MQTT_SERVER'), env('MQTT_PORT'), $client_id);
        $mqtt->connect(true, null, env('MQTT_USERNAME'), env('MQTT_PASSWORD'));

        $content = json_encode(
            [
                'type' => $push_type,
                'time' => (int)(microtime(true) * 1000),
                'msg' => $data,
            ]
        );
        if ($this->delay_seconds > 0) {
            $topic = '$delayed/' . $this->delay_seconds . '/' . $topic;
            $this->delay_seconds = 0;
        }
        $mqtt->publish($topic, $content, 2);
    }

    //相亲房推送 帽子  和 图片
    public function dating($data)
    {
        $topic = $this->topic_room;
        $data = [];
        $this->push(self::PUSH_POPULARITY, $topic, $data);
    }

    //推送人气
    public function updatePopularity($popularity)
    {
        $topic = $this->topic_room;
        $this->setDelay(1);
        $data = ['popularity' => $popularity, 'room_id' => $this->room_id];
        $this->push(self::PUSH_POPULARITY, $topic, $data);
    }

    //推送上麦
    public function pitOn($pit_number, $data)
    {
        $topic = $this->topic_room;
        $data['room_id'] = $this->room_id;
        $data['pit_number'] = $pit_number;

        $ball = app('redis')->exists('room:user:ball:' . $this->room_id . ':' . $this->user_id);
        $data['ball_state'] = !$ball ? 0 : 1;

        $data['auction_number'] = 0;
        $sys_type_id = app('model_user_room')->getRoomSystemTypeId($this->room_id);
        if (app('model_user_room')->isPmRoom($sys_type_id)) {
            $auction = app('model_auction')->select(['id', 'auction_gift_id'])->where([
                'room_id' => $this->room_id,
                'status' => 1,
            ])->orderBy('id', 'desc')->first();
            if ($auction) {
                $max = app('model_auction_record')->where([
                    'auction_id' => $auction->id,
                    'user_id' => $this->user_id,
                ])->orderBy('id', 'desc')->value('total_cost');
                if ($max > 0) {
                    $gift_price = app('model_auction_gift')->where(['id' => $auction->auction_gift_id])->value('gift_price');
                    $data['auction_number'] = $gift_price > 0 ? round($max / $gift_price) : 0;
                }
            }
        }
        $logRtx = new LogRepository("pitOn");
        $logRtx->log("request", json_encode($data));
        $logRtx->File();
        $this->push(self::PUSH_PIT_ON, $topic, $data);
    }

    //推送下麦
    public function pitDown($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'user_id' => $this->user_id];
        $this->push(self::PUSH_PIT_DOWN, $topic, $data);
    }

    //带坐骑用户进场特效通知
    public function ride($ride_info)
    {
        $topic = $this->topic_room;
        $this->setDelay(1);
        $data = [
            'room_id' => $this->room_id,
            'ride_url' => $ride_info['special'],
            'show_type' => $ride_info['show_type'],
        ];
        $this->push(self::PUSH_RIDE, $topic, $data);
    }

    //带爵位用户进场特效通知
    public function nobility($data)
    {
        $topic = $this->topic_room;
        $this->setDelay(1);
        if ($data['nobilityId'] == 6) {
            $special = ''; // 爵位特效地址
        } else {
            $special = '';
        }
        $data = [
            'room_id' => $this->room_id,
            'user_id' => $this->user_id,
            'nobility_name' => $data['nobilityName'],
            'nobility_id' => $data['nobilityId'],
            'nobility_icon' => $data['nobility_icon'],
            'avatar' => $data['headPicture'],
            'nickname' => $data['userName'],
            'special' => $special,
            'sex' => $data['sex'],
        ];
        $this->push(self::PUSH_NOBILITY, $topic, $data);
    }

    //上麦申请人数变化通知
    public function applyCount($count, $user_ids = '')
    {
        $topic = $this->topic_room;

        $count_8 = app('model_room_pit_apply')->where(['room_id' => $this->room_id, 'pit_number' => 8])->count();
        $count_8 = $count_8 > 0 ? $count_8 : 0;

        $total_count = $count - $count_8;
        $data = [
            'room_id' => $this->room_id,
            'count' => $total_count > 0 ? $total_count : 0,
            'count_8' => $count_8,
            'user_ids' => $user_ids,
        ];
        $this->push(self::PUSH_APPLY_COUNT, $topic, $data);
    }

    //用户被禁言 action 1禁言2解禁
    public function bannedUser($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id, 'action' => $action];
        $this->push(self::PUSH_BANNED_USER, $topic, $data);
    }

    //是否封麦 1.封麦 2.解除封麦
    public function closePit($pit_number, $action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'action' => $action];
        $this->push(self::PUSH_CLOSE_PIT, $topic, $data);
    }

    //清空单个麦位心动值
    public function clearCardiac($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_CLEAR_CARDIAC, $topic, $data);
    }

    //清空所有麦位心动值
    public function clearCardiacAll()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id];
        $this->push(self::PUSH_CLEAR_CARDIAC_ALL, $topic, $data);
    }

    //设置房间管理员
    public function setManager()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_SET_MANAGER, $topic, $data);
    }

    //删除房间管理员
    public function deleteManager()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_DELETE_MANAGER, $topic, $data);
    }

    //开关麦 action 1开0关
    public function switchVoice($action, $pit_number = 0)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'user_id' => $this->user_id,
            'pit_number' => $pit_number,
            'action' => $action,
        ];
        $this->push(self::PUSH_SWITCH_VOICE, $topic, $data);
    }

    //横幅礼物通知
    public function giftBanner($gift_list)
    {
        $topic = 'room';
        $data = ['room_id' => $this->room_id, 'list' => $gift_list];
        $this->push(self::PUSH_GIFT_BANNER, $topic, $data);
    }

    //聊天室礼物通知
    // public function giftChatRoom($gift_list, $cardiac, $contribution, $show_cat = 0)
    // {
    //     $topic = $this->topic_room;
    //     $data = [
    //         'room_id'      => $this->room_id,
    //         'gift_list'    => $gift_list,
    //         'cardiac_list' => $cardiac,
    //         'contribution' => $contribution,
    //         'show_cat'     => $show_cat,
    //         'user_id'      => $this->user_id,
    //     ];
    //     $this->push(self::PUSH_GIFT_CHAT_ROOM, $topic, $data);
    // }

    public function giftChatRoom($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_GIFT_CHAT_ROOM, $topic, $data);
    }

    public function rebateGiftChatRoom($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_REBATE_GIFT_GIVE, $topic, $data);
    }

    //盲盒礼物聊天室礼物通知
    public function blindBoxChatRoom($sendGift, $acceptGiftList, $cardiac, $contribution, $user, $show_cat = 0)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'blind_box_gift' => $sendGift,
            'head_picture' => $user->avatar,
            'gift_list' => $acceptGiftList,
            'cardiac_list' => $cardiac,
            'contribution' => $contribution,
            'show_cat' => $show_cat,
            'user_id' => $this->user_id,
        ];
        $this->push(self::PUSH_BLIND_BOX_GIFT_CHAT_ROOM, $topic, $data);
    }

    //聊天室礼物通知
    public function giftChatAuctionRoom($gift_list)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'gift_list' => $gift_list,
            'cardiac_list' => [],
            'contribution' => 0,
            'show_cat' => 0,
            'user_id' => $this->user_id,
        ];
        $this->push(self::PUSH_GIFT_CHAT_ROOM, $topic, $data);
    }

    //小猫钓鱼钓到大礼物时通知
    public function fish($gift_list, $user_info)
    {
        $txt = "<font color='#FFFFFF'>哇塞</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在小猫钓鱼中获得</font>";
        $txt_extra = [];
        foreach ($gift_list as $key => $value) {
            if ($value['price'] >= 99999) {
                $txt_extra [] = [
                    'text' => $txt . "<font color='#FABA5C'>" . $value['prize_title'] . "</font><font color='#FFFFFF'>X" . $value['number'] . "</font>",
                    'picture' => $value['picture'],
                ];
            }
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_FISH, $topic, $txt_extra);
        }
    }

    //房间密码 0取消密码1设置或修改密码
    public function roomPassword($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_ROOM_PASSWORD, $topic, $data);
    }

    //房间心动值开关 1开2关
    public function cardiacSwitch($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_CARDIAC_SWITCH, $topic, $data);
    }

    //上麦模式变化 1自由2排麦
    public function roomWheat($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_ROOM_WHEAT, $topic, $data);
    }

    //修改房间名称
    public function updateRoomName($room_name)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'room_name' => $room_name];
        $this->push(self::PUSH_UPDATE_ROOM_NAME, $topic, $data);
    }

    //周星用户进场
    public function weekStar($nickname, $rank)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'nickname' => $nickname, 'rank' => $rank];
        $this->push(self::PUSH_WEEK_STAR, $topic, $data);
    }

    //修改房间背景
    public function updateRoomBackground($background, $special = '')
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'background' => $background, 'special' => $special];
        $this->push(self::PUSH_UPDATE_ROOM_BACKGROUND, $topic, $data);
    }

    //修改房间公告
    public function updateRoomPlaying($playing)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'playing' => $playing];
        $this->push(self::PUSH_UPDATE_ROOM_PLAYING, $topic, $data);
    }

    //boss大作战 超过99999金币 发送所有人消息

    public function AttackBoss($gift_list, $user_info)
    {
        $txt = "<font color='#FFFFFF'>哇塞</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在BOSS大作战中获得</font>";
        $txt_extra = [];
        foreach ($gift_list as $key => $value) {
            if ($value['price'] >= 52000) {
                $txt_extra [] = [
                    'text' => $txt . "<font color='#FABA5C'>" . $value['prize_title'] . "</font><font color='#FFFFFF'>X" . $value['number'] . "</font>",
                    'picture' => $value['picture'],
                ];
            }
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_BOSS_ATTACK, $topic, $txt_extra);
        }
    }

    //boss大作战血量变化推送
    public function bossBlood($left_blood = 0, $total_blood = 0, $gift_list = [], $user_info = [])
    {
        if ($gift_list) {
            foreach ($gift_list as $key => $value) {
                if ($value['price'] >= 5200) {
                    $gift_list[$key]['nickname'] = $user_info['nickname'];
                } else {
                    unset($gift_list[$key]);
                }
            }
        }
        $topic = 'boss';
        $data = ['left_blood' => $left_blood, 'total_blood' => $total_blood, 'gift_list' => array_values($gift_list)];
        $this->push(self::PUSH_BOSS_BLOOD, $topic, $data);
    }

    //定向推送给上麦的用户
    public function agreeApplyUser($pit_number)
    {
        $topic = $this->topic_client;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_APPLY_USER, $topic, $data);
    }

    //定向推送给被踢出房间的用户
    public function kickOut()
    {
        $topic = $this->topic_client;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_KICK_OUT, $topic, $data);
    }

    //用户禁麦 action 1禁麦2解禁
    public function shutUp($action, $pit_number)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'action' => $action,
            'pit_number' => $pit_number,
            'user_id' => $this->user_id,
        ];
        $this->push(self::PUSH_SHUT_UP, $topic, $data);
    }

    //用户进入房间
    public function joinRoom($nickname, $rank_icon, $role, $user_is_new)
    {
        $topic = $this->topic_room;

        //用户个性装扮：进场特效
        $special = RedisService::getJoinSpecial($this->user_id);

        $data = [
            'room_id' => $this->room_id,
            'user_id' => $this->user_id,
            'nickname' => $nickname,
            'rank_icon' => $rank_icon,
            'role' => $role,
            'user_is_new' => $user_is_new,
            'background' => $special['picture'] ?? '',
            'color' => $special['special'] ?? '',
        ];

        $this->push(self::PUSH_JOIN_ROOM, $topic, $data);
    }

    //麦位倒计时
    public function countDown($pit_number, $seconds)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'seconds' => $seconds];
        $this->push(self::PUSH_COUNT_DOWN, $topic, $data);
    }

    //扔骰子
    public function roll($number, $pit_number)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'user_id' => $this->user_id,
            'number' => $number,
            'pit_number' => $pit_number,
        ];
        $this->push(self::PUSH_ROLL, $topic, $data);
    }

    //电台房开通黄金守护
    public function fmGold($nickname_from, $nickname_to)
    {
        //        $topic = 'room';
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'nickname_from' => $nickname_from, 'nickname_to' => $nickname_to];
        $this->push(self::PUSH_FM_GOLD, $topic, $data);
    }

    //发送表情
    public function face($pit_number, $picture, $special)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'pit_number' => $pit_number,
            'picture' => $picture,
            'special' => $special,
        ];
        $this->push(self::PUSH_FACE, $topic, $data);
    }

    //发送互动表情
    public function interaction_face($pit_start, $pit_end, $picture, $special_start, $special_start_time, $special_running, $special_end, $special_end_time)
    {
        // 起始麦位 终点麦位 表情三张图 起始和结束的运行时间 房间id
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'pit_start' => $pit_start,
            'pit_end' => $pit_end,
            'picture' => $picture,
            'special_start' => $special_start,
            'special_start_time' => $special_start_time,
            'special_running' => $special_running,
            'special_running_time' => 0,
            'special_end' => $special_end,
            'special_end_time' => $special_end_time,
            'user_id' => $this->user_id,
        ];
        $this->push(self::PUSH_INTERACTION_FACE, $topic, $data);
    }

    //上传即构日志
    public function zegoLog()
    {
        $topic = $this->topic_client;
        $data = ['user_id' => $this->user_id];
        $this->push(self::PUSH_ZEGO_LOG, $topic, $data);
    }

    //公屏状态
    public function roomChatStatus($status)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'status' => $status];
        $this->push(self::PUSH_ROOM_CHAT_STATUS, $topic, $data);
    }

    //球球大作战-开球
    public function ballStart($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_BALL_START, $topic, $data);
    }

    //球球大作战-弃球
    public function ballThrow($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_BALL_THROW, $topic, $data);
    }

    //球球大作战-亮球
    public function ballShow($pit_number, $first, $second, $third)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'pit_number' => $pit_number,
            'first' => $first,
            'second' => $second,
            'third' => $third,
            'user_id' => $this->user_id,
        ];
        $this->push(self::PUSH_BALL_SHOW, $topic, $data);
    }

    //房间音效改变
    public function soundEffectChange($sound_effect_detail)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
            'id' => $sound_effect_detail['id'],
            'config' => $sound_effect_detail['config'],
        ];
        $this->push(self::PUSH_SOUND_EFFECT_CHANGE, $topic, $data);
    }

    //用户下单--给大神推送通知消息
    public function sendOrderMessage($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_ORDER_RECEIVE, $topic, $data);
    }

    //用户申请退款--给大神推送通知消息
    public function sendRefundMessage($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_ORDER_REFUND, $topic, $data);
    }

    //派单厅-推送给 老板
    public function roomDemandToBoss($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_ROOM_DEMAND_BOSS, $topic, $data);
    }

    //派单厅-更新派单需求
    public function roomDemandUpdate($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_ROOM_DEMAND_UPDATE, $topic, $data);
    }

    //派单厅-将派单需求推送给所有符合条件的大神
    /*public function roomDemandToAnchor($data)
    {
        $topic = 'room';
        $this->push(self::PUSH_ROOM_DEMAND_TO_ANCHOR, $topic, $data);
    }*/

    public function roomDemandToAnchor($data, $topic)
    {
        $this->push(self::PUSH_ROOM_DEMAND_TO_ANCHOR, $topic, $data);
    }

    //订单消息推送(简单的消息浮窗)
    public function pushOrderMessage($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_ORDER_MESSAGE, $topic, $data);
    }

    //推送房间-拍卖开始
    public function startAuction($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_START_AUCTION, $topic, $data);
    }

    //推送房间-拍卖报价
    public function offerAuction($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_OFFER_AUCTION, $topic, $data);
    }

    //推送房间-拍卖流拍
    public function abortAuction($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_ABORT_AUCTION, $topic, $data);
    }

    //推送房间-拍卖成功
    public function endAuction($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_END_AUCTION, $topic, $data);
    }

    //人脉-CP绑定申请
    public function cpApply($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_CONNECTION_CP_APPLY, $topic, $data);
    }

    //人脉-结婚申请
    public function marryApply($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_CONNECTION_MARRY_APPLY, $topic, $data);
    }

    //人脉-结婚申请
    public function publishDraw($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_SINGLE_DRAW, $topic, $data);
    }

    //人脉-阶级四进入房间推送
    public function joinroomNotice($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_JOINROOM_NOTICE, $topic, $data);
    }

    //推送房间所有用户-开奖结果
    public function drawResult($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_DRAW_RESULT, $topic, $data);
    }

    //推送房主-开奖结果
    public function drawResultOwner($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_DRAW_RESULT_OWNER, $topic, $data);
    }

    // 推送话题广场抢头条通知
    public function themeStriveHeadline($data)
    {
        $this->push(self::PUSH_THEME_STRIVE_HEADLINE, "theme_push", $data);
    }

    // 推送话题广场参与人数变化
    public function themeJoinUserNumber($data)
    {
        $this->push(self::PUSH_THEME_JOIN_USER_NUMBER, "theme_push", $data);
    }

    //推送房间用户-主播更新点歌状态1开启2关闭
    public function songSwitch($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_SONG_SWITCH, $topic, $data);
    }

    //单人房贵宾人数通知
    public function nobilityCount()
    {
        $topic = $this->topic_room;
        $count = app('repo_single_room')->nobilityUserCount($this->room_id);
        $data = [
            'room_id' => $this->room_id,
            'count' => $count,
        ];
        $this->push(self::PUSH_NOBILITY_COUNT, $topic, $data);
    }

    //单人房歌单变化通知
    public function singleSongUpdate()
    {
        $topic = $this->topic_room;
        $data = [
            'room_id' => $this->room_id,
        ];
        $this->push(self::PUSH_SINGLE_SONG_UPDATE, $topic, $data);
    }

    public function finishWish($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_FINISH_WISH, $topic, $data);
    }

    //房间音效互动
    public function roomInteractinSound($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_INTERACTION_SOUND, $topic, $data);
    }

    //推送首次结缘CP
    public function cpFirstBind($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_CP_FIRST_BIND, $topic, $data);
    }

    //v3.5新人脉礼物特效
    public function connectGiftSpecial($data)
    {
        $topic = $this->topic_room;
        $this->push(self::PUSH_CONNECT_GIFT, $topic, $data);
    }

    //v3.5新人脉推送申请结缘
    public function applyConnect($pushData)
    {
        $topic = $this->topic_client;
        $pushData['type'] = self::PUSH_RELATION_APPLY;
        $this->push(self::PUSH_RELATION_APPLY, $topic, $pushData);
    }

    public function reportLog($pushData)
    {
        $topic = $this->topic_client;
        $pushData['type'] = self::PUSH_REPORT_LOG;
        $this->push(self::PUSH_REPORT_LOG, $topic, $pushData);
    }

    //我要上公屏结束公示推送
    public function screenActivityEnd($pushData)
    {
        $topic = $this->topic_room;
        $pushData['type'] = self::PUSH_SCREEN_ACTIVITY_END;
        $this->push(self::PUSH_SCREEN_ACTIVITY_END, $topic, $pushData);
    }

    //我要上公屏结束公示推送
    public function screenActivityOpen($pushData)
    {
        $topic = $this->topic_room;
        $pushData['type'] = self::PUSH_OPENED_SCREEN_ACTIVITY;
        $this->push(self::PUSH_OPENED_SCREEN_ACTIVITY, $topic, $pushData);
    }

    //国庆节游戏推送
    public function activity_game($picture, $txt)
    {
        $txt_extra[] = [
            'text' => $txt,
            'picture' => $picture,
        ];
        $topic = 'room';
        $this->push(self::PUSH_FISH, $topic, $txt_extra);
//        if (!empty($txt_extra)) {
//            $topic = 'room';
//            $this->push(self::PUSH_FISH, $topic, $txt_extra);
//        }
    }


    // 许愿树获得大礼物时通知
    public function lottery($plateName, $gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FFFFFF'>恭喜船长</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在{$plateName} 寻得</font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_PIRATE, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function gashaponNew($gift_list, $user_info, $levelInfo)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FFFFFF'>" . $levelInfo->name . " </font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'> 在奇遇锦囊中意外获得 </font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#F783FF'>" . $gift->price . "金币的</font>" . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $txt_extra[count($txt_extra) - 1]['text'] .= "<font color='#FFFFFF'>，震摄八方！</font>";
            $topic = 'room';
            $this->push(self::BROADCAST_NEW_GAME, $topic, $txt_extra);
        }
    }

    public function lottery3($gift_list, $user_info)
    {
        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>驾驶时空穿梭机前往过去/未来，途中获得礼物</font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_GASHAPON, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function gashapon($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FFFFFF'>恭喜用户</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在霓虹扭蛋机中 扭到</font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_GASHAPON, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function lottery2Score($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#C3B9FF'>恭喜</font><font color='#F783FF'>" . $user_info['nickname'] .
            "</font><font color='#C3B9FF'>少侠，打开怒气宝箱，获得礼物</font>";

        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#F783FF'>价值" . $gift->price . "金币</font><font color='#C3B9FF'>的至宝</font><font color='#FFFFFF'>" . $gift->name . "X" . $gift->count . "</font><font color='#C3B9FF'>，珠光宝气</font>",
                'picture' => $gift->picture,
            ];
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_LUCKY_TREE, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function lottery2($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#47B79E'>恭喜</font>" .
            "<b><font color='#47B79E'>{$user_info['nickname']}</font></b>" .
            "<font color='#47B79E'>少侠，成功击败董卓，获得礼物</font>" .
            "<font color='#47B79E'>，名扬天下</font>";

        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<b><font color='#00FDFF'>价值" . $gift->price . "金币</font></b><font color='#47B79E'>的礼物</font><b><font color='#00FDFF'>{$gift->name}X{$gift->count}</font></b><font color='#47B79E'>，名扬天下</font>",
                'picture' => $gift->picture,
            ];
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_LUCKY_TREE, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function blindBoxReceive($ext, $gift, $blindBoxList, $roomName, $acceptUserInfo)
    {
        $txt = "<font color='#B361FF'>{$acceptUserInfo['nickname']}</font><font color='#060606'>在</font><font color='#39DED0'>{$roomName}</font><font color='#060606'>打开了</font><font color='#6CCE4B'>{$ext['nickname']}</font>";
        $txt .= "<font color='#060606'>赠送的</font><font color='#5577FD'>{$gift['name']}</font>";
        $txt .= "<font color='#060606'>中开出价值</font>";
        $txt_extra = [];

        foreach ($blindBoxList as $blindBoxGift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#2FE5FF'>{$blindBoxGift['price']}</font><font color='#060606'>金币的</font>
                        <font color='#2FE5FF'>{$blindBoxGift['gift_name']}X{$blindBoxGift['number']}</font><font color='#060606'> ！</font>",
                'picture' => $blindBoxGift['picture'],
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_LUCKY_TREE, $topic, $txt_extra);
        }
    }

    public function blindBoxSend($roomName, $blindBoxList)
    {
        $txt = "<font color='ECE9FF'>在</font>";
        $txt .= "<font color='#FF8DF8'>{$roomName}</font>";
        $txt .= "<font color='#ECE9FF'>房间送出</font>";

        $txt_extra = [];

        foreach ($blindBoxList as $blindBoxGift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#2FE5FF'>{$blindBoxGift['name']} x {$blindBoxGift['count']}</font>",
                'picture' => $blindBoxGift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_LUCKY_TREE, $topic, $txt_extra);
        }
    }

    //转账推送
    public function transfer(int $senderUserId, int $targetUserId, int $coin, string $desc, int $transfer_type = 1)
    {
        $this->setSenderId('user_' . $senderUserId);
        $topic = 'user_' . $targetUserId;
        $data['content'] = [
            'type' => self::PUSH_TRANSFER,
            'coin' => $coin,
            'desc' => $desc,
            'transfer_type' => $transfer_type,
        ];
        $this->pushMessage($topic, $data, self::MSG_TYPE_TRANSFER);
    }

    //速配聊天
    public function groupMatch(int $senderUserId, int $targetUserId, $avatar, array $interesting)
    {
        $this->setSenderId('user_' . $senderUserId);
        $topic = 'user_' . $targetUserId;
        $data['content'] = [
            'type' => self::PUSH_MATCH,
            'user_id' => $senderUserId,
            'avatar' => $avatar,
            'interesting' => $interesting,
        ];
        return $this->pushMessage($topic, $data, self::MSG_TYPE_DATING);
    }

    public function groupMatchSend($userId, $targetId, $progress)
    {
        $data = [
            "target_id" => $targetId,
            "progress" => $progress,
        ];
        $this->push(self::PUSH_GROUP_MATCH_PROGRESS, 'user_' . $userId, $data);
    }

    public function getSenderId(): string
    {
        return $this->senderId;
    }

    public function setSenderId(string $senderId): void
    {
        $this->senderId = $senderId;
    }

    private function pushMessage($topic, $data, $msType)
    {
        $rcService = app('rongcloud');
        $delay = 0;
        if ($this->delay_seconds > 0) {
            $delay = $this->delay_seconds;
        }
        $senderId = $this->getSenderId();
        $rcService->queue($senderId, $topic, $data, $delay, $msType);
    }

    // 许愿树获得大礼物时通知
    public function luckyRebateGift($gifts, $user, $target, $coin)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FFFFFF'>恭喜用户</font><font color='#FD8469'>{$user['nickname']}</font><font color='#FFFFFF'>给</font>";
        $txt .= "<font color='#FD8469'>{$target['nickname']}</font>";
        $txt .= "<font color='#FFFFFF'>送出</font>";
        $txt_extra = [];
        foreach ($gifts as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>暴击获得" . $coin . "金币</font>",
                'picture' => $gift->picture,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_LUCKY_TREE, $topic, $txt_extra);
        }
    }

    public function ktvSingEnd($shareToken)
    {
        $pushData = [
            'room_id' => $this->room_id,
            'share_token' => $shareToken
        ];
        $topic = $this->topic_client;
        $pushData['type'] = self::PUSH_KTV_SING_END;
        $this->push(self::PUSH_KTV_SING_END, $topic, $pushData);
    }

    public function getGroup($targetId)
    {
        return "group_" . $targetId;
    }

    public function groupJoin(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_JOIN;
        $this->push(self::PUSH_GROUP_JOIN, $topic, $pushData);
    }

    public function groupQuit(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_QUIT;
        $this->push(self::PUSH_GROUP_QUIT, $topic, $pushData);
    }

    public function groupKickOut(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_KICK_OUT;
        $this->push(self::PUSH_GROUP_KICK_OUT, $topic, $pushData);
    }

    public function groupDisband(int $groupId, $operator)
    {
        $pushData = [
            'operator' => $operator,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_DISBAND;
        $this->push(self::PUSH_GROUP_DISBAND, $topic, $pushData);
    }


    public function groupBanned(int $groupId, $operator)
    {
        $pushData = [
            'operator' => $operator,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_BANNED;
        $this->push(self::PUSH_GROUP_BANNED, $topic, $pushData);
    }

    public function groupUnBanned(int $groupId, $operator)
    {
        $pushData = [
            'operator' => $operator,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_UNBANNED;
        $this->push(self::PUSH_GROUP_UNBANNED, $topic, $pushData);
    }

    public function groupMemberBanned(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_MEMBER_BANNED;
        $this->push(self::PUSH_GROUP_MEMBER_BANNED, $topic, $pushData);
    }

    public function groupMemberUnBanned(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_MEMBER_UNBANNED;
        $this->push(self::PUSH_GROUP_MEMBER_UNBANNED, $topic, $pushData);
    }

    public function groupCreateManager(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_CREATE_MANAGER;
        $this->push(self::PUSH_GROUP_CREATE_MANAGER, $topic, $pushData);
    }

    public function groupRemoveManager(int $groupId, $operator, array $users)
    {
        $pushData = [
            'operator' => $operator,
            'users' => $users,
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_REMOVE_MANAGER;
        $this->push(self::PUSH_GROUP_REMOVE_MANAGER, $topic, $pushData);
    }

    public function groupUpdate(int $groupId, $operator, array $data = [])
    {
        $pushData = [
            'operator' => $operator,
            "content" => $data
        ];
        $topic = $this->getGroup($groupId);
        $pushData["type"] = self::PUSH_GROUP_UPDATE_NAME;
        $this->push(self::PUSH_GROUP_UPDATE_NAME, $topic, $pushData);
    }

    public function lotteryCrystal($user, $prizes, $showLevel)
    {
        $txt = "<font color='#FFFFFF'>雪威哀嚎了！{$user->nickname} 攻击雪威时";
        $txt_extra = [];
        foreach ($prizes as $prize) {
            $txt_extra [] = [
                'text' => $txt . "得到了价值 {$prize->coin}金币的 {$prize->name} 宝物</font><font color='#FABA5C'>{$prize->name}</font><font color='#FFFFFF'>X {$prize->count}</font><font color='#FFFFFF'>光耀全服</font>",
                'picture' => $prize->icon,
                "level" => $showLevel,
            ];

        }
        if (!empty($txt_extra)) {
            $topic = 'room';
//            $this->push(self::BROADCAST_LOTTERY_CRYSTAL, $topic, $txt_extra);
        }
    }

    public function lotteryCrystalCrit($user, $prize, $showLevel)
    {
        $txt = "<font color='#FFFFFF'>雪威被击败了！{$user->nickname} 攻击雪威时发生 {$prize->crit} 倍暴击！";
        $txt .= "得到了价值 {$prize->coin} 金币的 {$prize->name} x {$prize->count}, 极度闪耀</font>";
        $txt_extra [] = [
            'text' => $txt,
            'picture' => $prize->icon,
            "level" => $showLevel,
        ];

        if (!empty($txt_extra)) {
            $topic = 'room';
//            $this->push(self::BROADCAST_LOTTERY_CRYSTAL_CRIT, $topic, $txt_extra);
        }
    }


    // 许愿树获得大礼物时通知
    public function lotteryPet($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FD8469'>" . mb_substr($user_info['nickname'], 0, 1) . "**</font><font color='#FFFFFF'> 获得了价值 </font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#F783FF'>" . $gift->price . "金币的</font>" . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }

        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_NEW_GAME, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function lotterySenior($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FD8469'>" . mb_substr($user_info['nickname'], 0, 1) . "**</font><font color='#FFFFFF'> 获得了价值 </font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#F783FF'>" . $gift->price . "金币的</font>" . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];

        }

        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_NEW_GAME, $topic, $txt_extra);
        }
    }


    // 许愿树获得大礼物时通知
    public function lotteryStellar($gift_list, $user_info)
    {
//        $txt = "<font color='#FFFFFF'>恭喜</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在浇树中获得：</font>";
        $txt = "<font color='#FD8469'>" . mb_substr($user_info['nickname'], 0, 1) . "**</font><font color='#FFFFFF'> 获得了价值 </font>";
        $txt_extra = [];
        foreach ($gift_list as $gift) {
            $txt_extra [] = [
                'text' => $txt . "<font color='#F783FF'>" . $gift->price . "金币的</font>" . "<font color='#FABA5C'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                'picture' => $gift->picture,
            ];
        }

        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::BROADCAST_NEW_GAME, $topic, $txt_extra);
        }
    }

    // 许愿树获得大礼物时通知
    public function petLuckyValuePush($luckyValue, $luckyCount)
    {
        $pushData = [
            'lucky_value' => $luckyValue,
            'lucky_count' => $luckyCount,
            'time' => (int)(microtime(true) * 1000),
        ];
        $topic = 'room';
        $this->push(self::BROADCAST_PET_LUCKY_VALUE, $topic, $pushData);
    }

    public function roomPKMatchFinish(int $pkId, int $userId)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => [
                "id" => $pkId,
                "target_users" => [
                    $userId,
                ],
                "tips" => "匹配成功，等待对方迎战！",
                "time" => (int)(microtime(true) * 1000),
            ],
        ];
        $this->push(self::PUSH_ROOM_PK_MATCH_FINISH, $topic, $pushData);
    }

    public function roomPKMatchNotFound(int $pkId, int $userId)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => [
                "id" => $pkId,
                "target_users" => [
                    $userId,
                ],
                "tips" => "未找到符合条件对手，请稍后再试！",
                "time" => (int)(microtime(true) * 1000),
            ],
        ];
        $this->push(self::PUSH_ROOM_PK_MATCH_NOTFOUND, $topic, $pushData);
    }

    public function roomPKMatchReply($pkModel, array $userIds, $pit_arr = [9])
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => [
                "id" => $pkModel->id,
                "pit_arr" => $pit_arr,
                "target_users" => $userIds,
                "room" => [
                    "id" => $pkModel->room_id,
                    "name" => $pkModel->room->room_name ?? "",
                    "cover" => $pkModel->room->cover_picture ?? "",
                    "score" => $pkModel->score,
                ],
                "match" => [
                    "id" => $pkModel->match_room_id,
                    "name" => $pkModel->match_room->room_name ?? "",
                    "cover" => $pkModel->match_room->cover_picture ?? "",
                    "score" => $pkModel->match_score,
                ],
                "second" => $pkModel->second,
                "time" => (int)(microtime(true) * 1000),
            ],
        ];
        $this->push(self::PUSH_ROOM_PK_MATCH_REPLY, $topic, $pushData);
    }

    public function roomPKMatchReject(int $pkId, int $userId)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => [
                "id" => $pkId,
                "target_users" => [
                    $userId,
                ],
                "tips" => "太强了！对方放弃了对决！",
                "time" => (int)(microtime(true) * 1000),
            ],
        ];
        $this->push(self::PUSH_ROOM_PK_MATCH_REPLY_REJECT, $topic, $pushData);
    }

    public function roomPKPlay($data, $public_room_id = 0)
    {
        $topic = $this->topic_room;
        $pushData = [
            "msg" => "房间对决开启！",
            "room_id" => $this->room_id,
            "public_room_id" => $public_room_id,
            "pk" => $data,
            // "pk" => [
            //     "id" => $pkModel->id,
            //     "type" => $pkModel->type,
            //     "room" => [
            //         "id" => $pkModel->room_id,
            //         "user_id" => $pkModel->room->holder, // 主持人
            //         "name" => $pkModel->room->room_name ?? "",
            //         "cover" => $pkModel->room->cover_picture ?? "",
            //         "score" => $pkModel->score,
            //     ],
            //     "match" => [
            //         "id" => $pkModel->match_room_id,
            //         "user_id" => $pkModel->match_room->holder, // 主持人
            //         "name" => $pkModel->match_room->room_name ?? "",
            //         "cover" => $pkModel->match_room->cover_picture ?? "",
            //         "score" => $pkModel->match_score,
            //     ],
            //     "start_time" => Carbon::make($pkModel->start_at)->timestamp,
            //     "second" => $pkModel->second,
            //     "countdown" => (time() <= Carbon::make($pkModel->start_at)->timestamp) ? 0 : $pkModel->second - time() - Carbon::make($pkModel->start_at)->timestamp,
            //     "time" => (int)(microtime(true)*1000),
            // ],
        ];
        $this->push(self::PUSH_ROOM_PK_PLAY, $topic, $pushData);
    }

    public function roomPKFinish($pk)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => $pk
        ];
        $this->push(self::PUSH_ROOM_PK_FINISH, $topic, $pushData);
    }

    public function roomPKScore($pkModel)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $pkModel->room_id,
            "pk" => [
                "id" => $pkModel->id,
                "room_id" => $pkModel->room_id,
                "score" => $pkModel->score,
                "match_room_id" => $pkModel->match_room_id,
                "match_score" => $pkModel->match_score,
                "contributors" => $pkModel->contributors,
                "match_contributors" => $pkModel->matchContributors,
                "time" => (int)(microtime(true) * 1000),
            ],
        ];
        $this->push(self::PUSH_ROOM_PK_SCORE, $topic, $pushData);
    }

    public function roomPitPKPlay($pk)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => $pk,
        ];
        $this->push(self::PUSH_ROOM_PIT_PK_PLAY, $topic, $pushData);
    }

    public function roomPitPKScore(int $pkId, int $redScore, int $blueScore)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => [
                "id" => $pkId,
                "red_score" => $redScore,
                "blue_score" => $blueScore,
            ],
        ];
        $this->push(self::PUSH_ROOM_PIT_PK_SCORE, $topic, $pushData);
    }

    public function roomPitPKFinish($pk)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => $pk,
        ];
        $this->push(self::PUSH_ROOM_PIT_PK_FINISH, $topic, $pushData);
    }

    public function roomPKMikeChange($mike)
    {
        $topic = $this->topic_room;
        $pushData = [
            "room_id" => $this->room_id,
            "pk" => $mike
        ];
        $this->push(self::PUSH_ROOM_PK_MIKE_CHANGE, $topic, $pushData);
    }

    public function broadcasts(array $data, int $delay = 0)
    {
        if (!empty($data)) {
            if ($delay > 0) {
                $this->setDelay($delay);
            }
            $this->push(self::PUSH_BROADCAST, "room", $data);
        }
    }

    public function pushAnchorRoom($room)
    {
        $topic = $this->topic_client;
        $data = [
            'content' => '',
            'room' => $room,
        ];
        $this->push(PushChatService::CHAT_RECOMMEND_ANCHOR_ROOM, $topic, $data);
    }

    public function pushAccompanyOrderCreate($info)
    {
        $topic = $this->topic_client;
        $data = [
            'content' => '',
            'info' => $info,
        ];
        $this->push(PushChatService::CHAT_RECOMMEND_ACCOMPANY_ORDER, $topic, $data);
    }

    public function updateRecordSeat($seat)
    {
        $topic = $this->topic_room;
        $roomUserGiftScoreService = new RoomUserGiftScoreService();
        $data = [
            "seats" => $roomUserGiftScoreService->roomSeats($this->room_id),
            'room_id' => $this->room_id,
        ];
        $this->push(self::PUSH_ROOM_CONNECTION_ONLINE, $topic, $data);
    }

    public function getTargetUser($userId): string
    {
        return "user_" . $userId;
    }

    public function accompanyOrderUnChat($targetUserId, $userId, $status, $msg = '')
    {
        $data = [
            "user_id" => $targetUserId,
            "status" => $status,
            "message" => $msg,
        ];

        $this->rcQueue(self::ORDER_UN_CHAT_ACCOMPANY, $this->getTargetUser($targetUserId), $data, 'RC:CmdMsg');

    }

    public function hallCollectedSpecial($user, $hall)
    {
        $data = [
            "room_id" => $this->room_id, // 房间id
            "special" => $hall->collect_special, // 特效文件
            "tips" => "恭喜{$user->nickname}集齐{$hall->title}", // 提示内容
            "user" => [ // 用户信息
                "id" => $user->user_id,
                "code" => $user->user_code,
                "nickname" => $user->nickname,
                "avatar" => $user->head_picture
            ],
        ];
        $topic = $this->topic_room;
        $this->push(self::PUSH_GIFT_HALL_COLLECT_SPECIAL, $topic, $data);
    }

    public function hallCollectedBanner($user, $hall, $status)
    {
        $colors = ($status == 1 ? $hall->lower_collect_banner_color : $hall->high_collect_banner_color) ?: [];
        $hallName = $hall->title . ($status == 1 ? "" : "（十套）");
        $achieve = $status == 1 ? "高级点亮" : "完美点亮";
        $url = $status == 1 ? $hall->lower_collect_banner : $hall->high_collect_banner;
        $colors = json_decode($colors, true);
        $defaultColor = $colors["default"] ?? "#FFFFFF";
        $userColor = $colors["user"] ?? "#FFE68C";
        $nameColor = $colors["name"] ?? "#FFFFFF";
        $taskColor = $colors["task"] ?? "#4AF8FE";

        $txt = "<font color='$defaultColor'>恭喜</font>";
        $txt .= "<font color='$userColor'>{$user->nickname}</font>";
        $txt .= "<font color='$defaultColor'>成功集齐</font>";
        $txt .= "<font color='$nameColor'>$hallName</font>";
        $txt .= "<font color='$defaultColor'>达成</font>";
        $txt .= "<font color='$taskColor'>{$achieve}</font>";
        $txt_extra [] = [
            'text' => $txt,
            'room_id' => $this->room_id,
            'picture' => '',
            'url' => $url,
        ];
        $topic = 'room';
        $this->push(self::PUSH_COMMON_BANNER, $topic, $txt_extra);
    }


    /**
     * 全服推送
     * @param array $notifyPushData 新运推送
     * @param array $broadcastPushData 全服飘屏推送
     * @param $extraPushData
     * @param array $roomPushData 房间大礼物推送
     * @param $uni_key
     * @param $getMillisecond
     * @throws \Exception
     */
    public function publicGamePush($notifyPushData, $broadcastPushData, $extraPushData, $roomPushData, $uni_key, $getMillisecond)
    {
        $topic = 'room';
        $ext = [
            'broadcastPush' => empty($broadcastPushData) ? (object)[] : $broadcastPushData,
            'notifyPush' => empty($notifyPushData) ? (object)[] : $notifyPushData,
            'extraPush' => empty($extraPushData) ? (object)[] : $extraPushData,
            'roomPush' => empty($roomPushData) ? (object)[] : $roomPushData,
            'uni_key' => $uni_key,
            'getMillisecond' => $getMillisecond
        ];
        if (empty($broadcastPushData) && empty($notifyPushData) && empty($extraPushData) && empty($roomPushData)) {
            return;
        }
        $this->pushV1(self::GAME_PUSH_STATUS, $topic, $ext);
        return;
    }

    public function changeUserGrade($data)
    {
        $topic = $this->topic_client;
        $this->push(self::PUSH_USER_GRADE, $topic, $data);
    }

    //碎片获取通知
    public function fragmentPush($data)
    {
        $topic = $this->topic_room;
        $this->push(self::GAME_PUSH_FRAGMENT, $topic, $data);
    }
}


